package com.molyfun.parents.activity.mainpage;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.adapter.MainAskPagerAdapter;
import com.molyfun.parents.activity.common.BaseMainTabFragment;
import com.molyfun.parents.im.ConsultActivity;
import com.molyfun.parents.impl.IPicUploadCallback;
import com.molyfun.parents.rest.bean.CreateCustomPlanResponse;
import com.molyfun.parents.rest.bean.CustomPlanlistResponse;
import com.molyfun.parents.utils.PreferencesStorageUtil;
import com.molyfun.parents.view.TwoTabBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class MainTabMainPageFragment extends BaseMainTabFragment implements ViewPager.OnPageChangeListener, IPicUploadCallback, TwoTabBar.TwoBarClickListener, View.OnClickListener{

    @InjectView(R.id.two_tab_bar)
    TwoTabBar mMainCourseNoticeView;
    @InjectView(R.id.topic_viewpager)
    ViewPager mFeedViewPager;
    @InjectView(R.id.consult)
    TextView mConsult;

    MainAskPagerAdapter mMainAskPagerAdapter;

    List<Fragment> topicFragmentList;
    MyPageFragment mMyPageFragment;
    TrainRecommendFragment mTrainRecommendFragment;
    private CustomPlanlistResponse.PlanListBean planListBean;
    private String imgroupid;
    private String TAG = "MainTabMainPageFragment";

    public static MainTabMainPageFragment newInstance() {
        return new MainTabMainPageFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (!PreferencesStorageUtil.isUserEnter(getContext())) {
            return super.onCreateView(inflater, container, savedInstanceState);
        }

        View view = inflater.inflate(R.layout.fragment_main_page, container, false);
        ButterKnife.inject(this, view);

        mMainCourseNoticeView.initData("基本信息", "咨询管理");
        mMainCourseNoticeView.setTwoBarClickListener(this);

        topicFragmentList = new ArrayList<>();
        mMyPageFragment = MyPageFragment.newInstance();
        topicFragmentList.add(mMyPageFragment);
        mTrainRecommendFragment = TrainRecommendFragment.newInstance();
        topicFragmentList.add(mTrainRecommendFragment);
        mMainAskPagerAdapter = new MainAskPagerAdapter(getChildFragmentManager(), topicFragmentList);
        mFeedViewPager.setAdapter(mMainAskPagerAdapter);
        mFeedViewPager.addOnPageChangeListener(this);
        mFeedViewPager.setCurrentItem(0);

        mConsult.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.consult:
                createCustomplan();
                break;
        }
    }

    public void createCustomplan() {
        Call<CreateCustomPlanResponse> call = MFPApplication.getInstance().getRestClient().createCustomplan();
        call.enqueue(new Callback<CreateCustomPlanResponse>() {
            @Override
            public void onResponse(Call<CreateCustomPlanResponse> call, Response<CreateCustomPlanResponse> response) {
                dismissProgressDialog();
                if (response.body() != null) {
                    if (response.body().getCode() == 200) {
                        if (response.body() != null && response.body().getData() != null) {
                            planListBean = response.body().getData();
                            imgroupid = response.body().getData().getImgroupid();
//                            ChatActivity.startActivity(getContext(), response.body().getData(), response.body().getData().getImgroupid());
                            ConsultActivity.startActivity(getContext(), planListBean, imgroupid);
                        }
                    } else {
                        toast(response.body().getMsg());
                    }
                }
            }

            @Override
            public void onFailure(Call<CreateCustomPlanResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });

    }

    @Override
    public void onPictureUploadBegin(String path, String UploadPath) {

    }

    @Override
    public void onPictureUploadFailed(String path, String UploadPath) {

    }

    @Override
    public void onPictureUploadSuccess(String localPath, String smallUrl, String originalUrl) {

    }

    @Override
    public String getAttachmentFolder() {
        return "mainpage";
    }

    @Override
    public void onVideoUploadSuccess(String cover, String path) {

    }

    @Override
    public void onParentClickTab() {

        if (getView() != null) {
            if (PreferencesStorageUtil.isUserEnter(getContext())) {
                switch (mMainCourseNoticeView.getCurrentChooseTab()) {
                    case TwoTabBar.TwoTabChooseTab.BAR_ONE:
                        if (mMyPageFragment != null) {
                            mMyPageFragment.onParentTabClick();
                        }
                        break;
                    case TwoTabBar.TwoTabChooseTab.BAR_TWO:
                        if (mTrainRecommendFragment != null) {
                            mTrainRecommendFragment.onParentTabClick();
                        }
                        break;
                }
            }
        }

    }

//    private List<AskTabOnResumeCallback> mAskTabOnResumeCallback;
//
//    public interface AskTabOnResumeCallback{
//        public void onAskResume();
//    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onTwoBarClick(int chooseTab) {
        switch (chooseTab) {
            case TwoTabBar.TwoTabChooseTab.BAR_ONE:
                mFeedViewPager.setCurrentItem(0);
                break;
            case TwoTabBar.TwoTabChooseTab.BAR_TWO:
                mFeedViewPager.setCurrentItem(1);
                break;
        }
    }

    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            mMainCourseNoticeView.setTabClick(TwoTabBar.TwoTabChooseTab.BAR_ONE);
        } else {
            mMainCourseNoticeView.setTabClick(TwoTabBar.TwoTabChooseTab.BAR_TWO);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}